// subpkg_worker/prescriptionDel/prescriptionDel.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderlyId:'',
    medicineList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      elderlyId:options.elderId
    })
    
  },
  query(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_list/query',
      data:{
        elderlyId:this.data.elderlyId
      },
      success:(res)=>{
        var medicineList = res.data.data
        console.log(medicineList)
        var medicineList1 = []
        for(var i in medicineList){
          for(var j in medicineList[i]){
            if(medicineList[i][j].term=='长期')
            {
              medicineList1.push(medicineList[i][j])
            }
          }
        }
        this.setData({
          medicineList:medicineList1
        })
        
      }
    })
  },
  add(){
    wx.navigateTo({
      url: `/subpkg_worker/prescriptionAdd/prescriptionAdd?elderId=${this.data.elderlyId}&term=长期`,
    })
  },
  delete(e){
    
   const that = this
    var index = e.currentTarget.dataset.index
    console.log(index)
    Dialog.confirm({
      title: '提示',
      message: `你确定要删除药物"${this.data.medicineList[index].medication.name}"吗`,
    })
      .then(() => {
        wx.request({
          url: `https://zhixiao.site:8080/medication-list/delete?id=${that.data.medicineList[index].id}`,
          method:'DELETE',
          success:(res)=>{
            console.log(res)
            Toast('删除成功')
          },
          complete:(res)=>{
            that.query()
          }
        })
      })
      .catch(() => {
        
      });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.query()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})